#ifndef WEBSERVER_H
#define WEBSERVER_H

#include "msg_queue_peer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <errno.h>
#include <unistd.h>
#include "cJSON.h"
#include "shmem.h"



#define PORT 8080
#define BUFFER_SIZE 4096
#define MSG_KEY 1234
#define SHM_KEY 5678

typedef struct {
    char method[16];
    char path[256];
    char protocol[16];
    char body[2048];
} HttpRequest;

typedef struct {
    long mtype;
    char mdata[256];
} msgbuf;

typedef struct {
    const char *device_id;  // 设备ID字符串
    const char *type;       // 设备类型字符串
    int key;                // 设备键值(整数)
} DeviceInfo;
// 温湿度数据结构
typedef struct {
    float temperature;
    float humidity;
} SensorData;

extern const DeviceInfo deviceControlMap[];
extern int msgid;
extern int g_shmid;
extern char* g_shm_ptr;

void parse_request(const char *buffer, HttpRequest *req);
void send_response(int sock, int code, const char *content_type, const char *body);
int handle_string_control(int sock, const char *command);
int handle_get(int sock, const char *input);
float get_sensor_data(const char *type);
void handle_device_status(int sock);
int hardware_control(int device_key, int action);
int get_hardware_status(int device_key);
int send_device_message(int device_key, int action);
int read_sensor_data(SensorData *data);

#endif /* WEBSERVER_H */